<script lang="ts" setup>
import { Calendar, Search, Delete } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
/**
 * 分类名称
 * 启用状态
 */
interface User {
  date: string
  name: string
  state: string
}

const tableData = ref([
  {
    date: '2016-05-03',
    name: '分类1',
    state: true,
  },
  {
    date: '2016-05-02',
    name: '分类2',
    state: false,
  },
])
const rules = reactive({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
  ],
  state: [
    { required: true, message: 'Please input Activity state', trigger: 'blur' },
  ],
  date: [
    { required: true, message: 'Please input Activity date', trigger: 'blur' },
  ],
})
let name = ref('')
let dialogFormVisible = ref(false)
let title = ref('')
let ruleForm = ref({
  name: '',
  state: '',
  address: '',
  date: '',
})
const ruleFormRef = ref<FormInstance>()

const search = () => {
  tableData.value = tableData.value.filter(item => item.name.includes(name.value))
}
const handleAdd = () => {
  title.value = '添加'
  dialogFormVisible.value = true
  ruleForm = ref({
    name: '',
    state: '',
    date: '',
  })
}
const globalIndex = ref(-1)
const handleEdit = (row, index) => {
  globalIndex.value = index
  title.value = '编辑'
  ruleForm.value = row
  dialogFormVisible.value = true
}

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (title.value === '添加') {
      console.log(ruleForm.value)
      tableData.value.push(ruleForm.value)
      dialogFormVisible.value = false
    } else if(title.value === '编辑'){
      tableData.value[globalIndex.value] = ruleForm.value
      dialogFormVisible.value = false
    }
  })
}

const handleDel = () => {
  console.log('del')
} 


const remove = (index) => {
  tableData.value.splice(index, 1)
}

</script>

<template>
    <div id="category">
        <el-card>
            <template #header>
                <div class="card-header">
                <span>文章分类</span>
                </div>
            </template>
            <div style="margin-bottom: 10px;">
              <el-input style="width:200px" v-model="name" placeholder="名称" :suffix-icon="Search"></el-input>
              <el-button type="primary" @click="search" style="margin-left:30px" :icon="Search">search</el-button>
              <el-button type="primary" plain @click="handleAdd">add</el-button>
              <el-button type="danger" plain @click="handleDel" :icon="Delete">del</el-button>
            </div>
            <el-table :data="tableData">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="date" label="Date"  />
                <el-table-column prop="name" label="Name"  />
                <el-table-column prop="state" label="State">
                  <template #default="{row}">
                    <el-switch
                    v-model="row.state"
                    inline-prompt
                    active-text="是"
                    inactive-text="否"
                    />
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template #default="scope">
                    <el-button type="primary" size="small" @click="handleEdit(scope.row,scope.$index)">edit</el-button>
                    <el-button type="danger" size="small" @click="remove(scope.$index)">del</el-button>
                  </template>
                </el-table-column>
            </el-table>
            <template #footer>Footer content</template>
        </el-card>
    </div>

    <!-- 拟态框 -->
    <el-dialog v-model="dialogFormVisible" :title='title' width="40%">
      <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="ruleForm.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="分类状态" prop="state">
          <el-select v-model="ruleForm.state" placeholder="请选择">
            <el-option label="已启用" :value="true"></el-option>
            <el-option label="不启用" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间" prop="date">
            <el-date-picker v-model="ruleForm.date" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                placeholder="请选择" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          Confirm
        </el-button>
      </template>
    </el-dialog>
</template>


<style lang="less" scoped>
#category {
    .el-card {
        margin: 20px;
    }
}
</style>